# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
import urllib
from jdSpider.items import ComputerItem
from selenium import webdriver


# https://search.jd.com/Search?
# keyword=笔记本&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&page=1&s=1&click=0    第一页
# keyword=笔记本&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&page=3&s=56&click=0   第二页
# keyword=笔记本&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&page=5&s=118&click=0  第三页
# keyword=笔记本&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&page=7&s=173&click=0  第四页
# keyword=笔记本&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&page=9&s=226&click=0  第五页
#



# https://search-x.jd.com/Search?
# callback=jQuery3604853&area=1&enc=utf-8&keyword=笔记本&adType=7&page=1&ad_ids=292%3A5&xtest=new_search&_=1535075285471


class JdcomputerSpider(scrapy.Spider):
    name = 'jdcomputer'
    allowed_domains = ['search.jd.com']
    url = "https://search.jd.com/Search?keyword=笔记本&enc=utf-8&qrst=1&rt=1&stop=1&click=0&vt=2&page="
    page = 1
    start_urls = [url + str(page)]

    def parse(self, response):
        for each in response.xpath("//div[@id='J_goodsList']/ul/li"):
            item = ComputerItem()
            # get_price(each)
            item["price"] = self.get_price(each.xpath(".//div[@class='p-price']/strong/i/text()").extract())
            item["title"] = self.get_title(each.xpath(".//div[@class='p-name p-name-type-2']/a/em/text()").extract())
            item["commentsCount"] = self.get_comments_count(each.xpath(".//div[@class='p-commit']//strong/a/text()").extract())
            item["shopName"] = self.get_shopName(each.xpath(".//span[@class='J_im_icon']/a/text()").extract())
            item["productUrl"] = self.get_productUrl(each.xpath(".//div[@class='p-name p-name-type-2']/a/@href").extract())
            item["source"] = "https://search.jd.com"
            yield item

        # 下一页
        self.page += 2
        yield scrapy.Request(url=self.url + str(self.page), callback=self.parse)


    def get_price(self, list):
        if len(list) > 0:
            price = list[0]
        else:
            price = 'null'
        return price.strip()

    def get_title(self, list):
        if len(list) > 0 :
            title = list[0]
        else:
            title = ""
        return title.strip()

    def get_comments_count(self, list):
        if len(list) > 0 :
            commentsCount = list[0]
        else:
            commentsCount = ""
        return commentsCount.strip()

    def get_shopName(self, list):
        if len(list) > 0 :
            shopName = list[0]
        else:
            shopName = ""
        return shopName.strip()

    def get_productUrl(self, list):
        if len(list) > 0 :
            productUrl = list[0]
        else:
            productUrl = ""
        return productUrl.strip()
